package org.boulderfoodrescue.webapp.client.old.datamanager;

import java.util.Date;

import org.boulderfoodrescue.webapp.client.old.event.PlaceChangedEvent;
import org.boulderfoodrescue.webapp.client.old.event.PlaceChangedHandler;
import org.boulderfoodrescue.webapp.client.old.viewer.ViewerService;
import org.boulderfoodrescue.webapp.client.old.viewer.ViewerServiceAsync;
import org.boulderfoodrescue.webapp.shared.Place;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class DataChangeListener {
  public DataChangeListener(){
    DataManager.get().addHandler(new PlaceChangedHandler(){

      @Override
      public void onPlaceAdded(PlaceChangedEvent event) {
        placeAdded(event.getPlace());
      }

      @Override
      public void onPlaceRemoved(PlaceChangedEvent event) {
        placeRemoved(event.getPlace());
      }

      @Override
      public void onPlaceUpdate(PlaceChangedEvent event) {
        placeUpdated(event.getPlace());
      }

    }, PlaceChangedEvent.getType());
  }
  public void placeAdded(final Place place){
    if(DataManager.getLoginInfo().isLoggedIn()){
      ViewerServiceAsync vService = GWT.create(ViewerService.class);
      AsyncCallback<Long> callback = new AsyncCallback<Long>(){
        
        @Override
        public void onFailure(Throwable caught) {
          System.out.println(caught.getMessage());

          Window.alert("Error saving place to server");
        }
        @Override
        public void onSuccess(Long result) {
          if(result == null)
            Window.alert("Error saving place");
          else{
            place.setSaved((new Date()).getTime());
            place.setKey(result);
          }
        }
      };
      vService.savePlace(place, callback);
    }
  }
  public void placeRemoved(final Place place)
  {
    if(DataManager.getLoginInfo().isLoggedIn()){
      ViewerServiceAsync vService = GWT.create(ViewerService.class);
      AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){
        @Override
        public void onFailure(Throwable caught) {
          Window.alert("Error deleting place from server");
        }

        @Override
        public void onSuccess(Boolean result) {
        }

      };
      vService.removePlace(place, callback);
    }
  }
  public void placeUpdated(final Place place)
  {
    if(DataManager.getLoginInfo().isLoggedIn()){
      ViewerServiceAsync vService = GWT.create(ViewerService.class);
      AsyncCallback<Long> callback = new AsyncCallback<Long>(){
        @Override
        public void onFailure(Throwable caught) {
          Window.alert("Error saving changes to place to server");
        }
        @Override
        public void onSuccess(Long result) {
          if(result == null)
            Window.alert("Error saving place");
          else{
            place.setSaved((new Date()).getTime());
            place.setKey(result);
          }
        }
      };
      vService.savePlace(place, callback);
    }
  }
}